<?php

/**
 * User controller
 *
 * @uses       Beaver_Controller_Action
 * @package    Beaver
 * @subpackage Controller
 *
 * @extdirect-crud
 */
class UserController extends Beaver_Controller_Action
{
	//private $_sessionLifetime = 30; //in second
	
	public function preDispatch()
	{
		$this->_crudModel = new Default_Model_User();
	}
	
	public function indexAction()
	{
		//$session = $this->getInvokeArg("bootstrap")->getOption("session");
		$this->view->lifetime = ini_get("session.gc_maxlifetime");
		//$this->view->lifetime = $this->_sessionLifetime;
	}

        /**
         * @extdirect-enable
         * @extdirect-formHandler
         */
	public function loginAction()
	{
		if(!isset($this->_request->extParams))
		{
			return;
		}
		$extParams = $this->_request->extParams;
		$filter = new Zend_Filter_StripTags();
		$username = $filter->filter($extParams["username"]);
		$password = $filter->filter($extParams["password"]);
		
		if ( empty($username) )
		{
			$this->view->success = false;
			$this->view->errors[] = array(
				"username" => "Please provide a username."
			);
		}
		if ( empty($password) )
		{
			$this->view->success = false;
			$this->view->errors[] = array(
				"password" => "Please provide a password."
			);
		}
		if ( count($this->view->errors) == 0 )
		{
			$db = $this->getInvokeArg("bootstrap")->getResource("db");

                        
			$authAdapter = new Zend_Auth_Adapter_DbTable($db);
			$authAdapter->setTableName("user");
			$authAdapter->setIdentityColumn("username");
			$authAdapter->setCredentialColumn("password");
			$authAdapter->setIdentity($username);
			$authAdapter->setCredential($password);
			
			// do the authentication
			$auth   = Zend_Auth::getInstance();
			$result = $auth->authenticate($authAdapter);
			if ($result->isValid())
			{
				$user = new Default_Model_User();
				$user->findByUsername($username);
				
				$data = $user->getAttributes();
				unset($data["password"]);
				$auth->getStorage()->write($data);
				$this->view->data = $data;
				$this->view->success = true;
				$user->eventLoggin();
			}
			else
			{
				$this->view->success = false;
			}
		}
	}

        /**
         * @extdirect-enable
         */
	public function logoutAction()
	{
		// force the event, because, the checksession javascript event will run
		// and the authentication panel will appear
		$this->_extResponseRender = true;
		$auth = Zend_Auth::getInstance();
		$auth->clearIdentity();
		$this->view->type = "event";
		$this->view->name = "checksession";
		$this->view->data = false;
	}
	
	/**
	 * Check the user is logged in. If user hasn't an active session, 
	 * the the "checksession" event will run and
         *
         * @extdirect-enable
	 */
	public function checksessionAction()
	{
		//var_dump($_SESSION);
		$auth = Zend_Auth::getInstance();
		$hasIdentity = $auth->hasIdentity();
		$this->_extResponseRender = true;
		// force the event, because if session lost, the checksession javascript event will run
		// and the authentication panel will appear
		$this->view->type = "event";
		$this->view->name = "checksession";
		
		if ($hasIdentity)
		{
			$this->view->data    = $auth->getIdentity();
		}
		else
		{
			$this->view->data    = false;
		}
	}	
}
